package com.ace.tlog

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ace.log.L
import com.ace.log.file.FileBuilder
import com.ace.log.file.clean.FileLastModifiedCleanStrategy
import com.ace.log.printer.ConsolePrinter

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.io.File


class MainViewModel : ViewModel() {

    var isAdd = false

    var loopJob: Job? = null

    init {
        L.setTag("CH").addPrinter(ConsolePrinter())
    }

    fun test() {
        L.d("日志d ${System.currentTimeMillis()}")
    }

    fun add() {
        val filePrinter = FileBuilder().folderPath("/storage/emulated/0/logs").build()
        L.addPrinter(filePrinter)
    }


    fun add2() {
        if (!isAdd) {
            isAdd = true
            val filePrinter = FileBuilder().folderPath("/storage/emulated/0/logs")
                .fileNameGenerator(DateWithSecondFileNameGenerator(30))
                .cleanStrategy(FileLastModifiedCleanStrategy(40 * 1000))
                .build()
            L.addPrinter(filePrinter)
        }
        loopJob?.cancel()
        loopJob = viewModelScope.launch {
            while (isActive) {
                L.d("循环输入 ${System.currentTimeMillis()}")
                delay(100)
            }
        }
    }

    fun zip() {
        viewModelScope.launch(Dispatchers.IO) {

            val result = File("/sdcard/logs").listFiles().asList()
            com.ace.log.file.zip.zip(result, "/sdcard/test.zip") {
                Log.d("CH", "压缩完成")
            }
        }
    }


}